package com.Action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.Bean.rolemenuModel;
import com.Bean.wdmenuModel;
import com.Bean.wduserModel;
import com.Service.rolemenuService;
import com.Service.wdmenuService;
import com.Service.wduserService;









@Controller
@RequestMapping("/user")/**分路径****/
public class wduserAction {
	@Autowired
	private wduserService wduserService;
	
	@Autowired
	private rolemenuService  rolemenuService;
	
	@Autowired
	private wdmenuService  wdmenuService;
	
	@RequestMapping("/list.do")
	public String list(Model model){
	  System.out.print("---->in!!");
	  List<wduserModel> list =wduserService.selectAll();
	   for(wduserModel uu:list){
		   
		   System.out.println("@@@@@@@@@@@"+uu.getUpdateTime());
	   }
	
		
  
	  model.addAttribute("list", list);
	  return "Employees";
	}
   
	@InitBinder
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
	}

	
	@RequestMapping("/name2.do")
	public @ResponseBody String toAjax2(String  yanz,HttpServletRequest request){
		
		System.out.println("<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  in name2.do");
		request.getSession().removeAttribute("message");
		String fhui="清除成功";
        return fhui;
	}
	
	
	
	@RequestMapping("/name.do")
	public @ResponseBody Map<String, Object> toAjax(String  yanz){
		
    wduserModel  wduser = wduserService.selectByname(yanz);
	  Map<String, Object> comMap = new HashMap<String, Object>();
	  comMap.put("complexList", wduser);
	  return comMap;
	}
	
	
	@RequestMapping("/number.do")
	public @ResponseBody Map<String, Object> toAjax3(String  yanz){
		
     wduserModel  wduser = wduserService.selectBynumber(yanz);
	  Map<String, Object> comMap = new HashMap<String, Object>();
	  comMap.put("complexList", wduser);
	  return comMap;
	}
	
	
	
	@RequestMapping("/userlogin.do")
	public String userlogin( wduserModel  wduserModel,HttpServletRequest request,Model model){
		 wduserModel  wduser = wduserService.selectByuser(wduserModel);
		if(wduser!=null){
			request.getSession().setAttribute("loginname", wduser.getName());
			request.getSession().setAttribute("loginrole", wduser.getRolId());
			
			
			List<rolemenuModel> list=rolemenuService.selectByUserID(wduser.getUserId());
			List<wdmenuModel>  wlist=new ArrayList<wdmenuModel>();
			
			if(list.size()>0){
				for(rolemenuModel role:list){
					wdmenuModel	wdmen=wdmenuService.selectByPrimaryKey(role.getMenuId());
					wlist.add(wdmen);
				}
			}
		
			model.addAttribute("wlist", wlist);
			
			
			return "index";
			
			
			
		}else{
			request.setAttribute("message", "用户名或者密码错误");
//			return "redirect:../index5.jsp";
			return  "index5";
		}
	}
	@RequestMapping("/Adduser.do")
	public String toAddProject1() {
		
		return "EmployeesUpdateManager";
	}
	
	
	
	@RequestMapping("/toAdduser.do")
	public String toAddProject(wduserModel  wduserModel,HttpServletRequest request) {
		String UpdateUser=(String) request.getSession().getAttribute("loginname");
		wduserModel.setUpdateUser(UpdateUser);
		     Date date=new Date();
			 wduserModel.setUpdateTime(date);
			 System.out.println("<<<<<<<<>>>>>>>>>>"+wduserModel);
	    int a=wduserService.insertSelective(wduserModel);
	    
	    
		/*if(a>0){
		String userID=wduserService.selectByname(wduserModel.getName()).getUserId();
	      List<wdmenuModel> listt=wdmenuService.selectChild("1");
	     for(wdmenuModel wdmen:listt){
	    	 System.out.println(" $$$$$$$$$$4 "+wdmen.getMenuId());
	    	 rolemenuModel rolemenu=new  rolemenuModel();
	    	 rolemenu.setMenuId(wdmen.getMenuId());
	    	 rolemenu.setUserId(userID);
	    	 rolemenuService.insertSelective(rolemenu);
	     }
	     
		}*/
	
		return "redirect:/user/list.do";
	}
	
	@RequestMapping("/tomohu.do")
	public String mohuchaxun(wduserModel  wduserModel,Model model){
		System.out.print("---->in to tomohu.do");
		System.out.println("用户名"+wduserModel.getName());
		List<wduserModel> list=wduserService.selectBymany(wduserModel);
		
		model.addAttribute("list", list);
		return "Employees";
	}
	
	
	
	
	@RequestMapping("/tomohu2.do")
	public String mohuchaxun2(wduserModel  wduserModel,Model model){
		System.out.print("---->in to tomohu2.do");
		System.out.println("用户名"+wduserModel.getName());
		if(wduserModel.getName()==""){
			wduserModel.setName(null);
			wduserModel.setRolId(0);
		}
		List<wduserModel> list=wduserService.selectBymany(wduserModel);
		
		model.addAttribute("list", list);
		return "quanxian";
	}
	/***删除操作***/
	@RequestMapping("/toudele.do")
	public String  userdele(String userID,HttpServletRequest request){
		System.out.println("In toudele.do "+"   "+userID);
		int a=wduserService.deleteByPrimaryKey(userID);
		System.out.println("a:"+"   "+a);
		if(a>0){
			
			request.getSession().setAttribute("message", "删除成功");
		}else{
			request.getSession().setAttribute("message", "删除失败");
		}
		
		 return "redirect:/user/list.do";
		 
	}
	/********修改操作***********/
	@RequestMapping("/updateuser.do")
	public String updateuser(String userID,Model model){
		System.out.println("in updateuser"+"  userID  "+userID);
		wduserModel wduser=wduserService.selectByPrimaryKey(userID);
        model.addAttribute("wduser", wduser);		
		return "Employeesxiugai";
	}
	
	
	
	
	
	@RequestMapping("/toUpdateuser.do")
	public String toUpdate(wduserModel  wduserModel,HttpServletRequest request){
		     System.out.println("in toUpdateuser.do"+"  userID  "+wduserModel);
		     String UpdateUser=(String) request.getSession().getAttribute("loginname");
			 wduserModel.setUpdateUser(UpdateUser);
		     Date date=new Date();
			 wduserModel.setUpdateTime(date);
		    int a= wduserService.updateByPrimaryKeySelective(wduserModel);	
		    if(a>0){
				
				request.getSession().setAttribute("message", "修改成功");
			}else{
				request.getSession().setAttribute("message", "修改失败");
			}  
		return "redirect:/user/list.do";
	}
	
	
	@RequestMapping("/quanxian.do")
	public String quanxian(){
		System.out.println("in quanxian"+"   ");
			
		return "quanxian";
	}
	
	
}
